package framework;

import interfaces.Board;
import interfaces.Cell;

/**
 * A Board represents a 2D grid of cells.
 * @author bcharna
 *
 */
public class BoardImpl implements Board {
	protected int width;
	protected int height;
	protected Cell[][] cells;

	/**
	 * Creates a new board filled with cells.  Dimensions of cells array
	 * must agree with width and height arguments.
	 * @param cells initials cells for Board to start with
	 * @param width width of board
	 * @param height height of board
	 * @throws InvalidCellsArrayException if width and height do not correspond
	 * to the dimensions of the 2D Cell array
	 */
	public BoardImpl(Cell[][] cells, int width, int height) throws InvalidCellsArrayException {
		
		// ensure outer array is right size
		if (cells.length != width) throw new InvalidCellsArrayException();
		// fill board with cells.
		for (int i = 0; i < width; i++)
			if (cells[i].length != height)
				throw new InvalidCellsArrayException();
		this.width = width;
		this.height = height;
		this.cells = cells;
	}
	
	
	@Override
	public Cell[][] getCells() {
		return cells;
	}
	
	/**
	 * 
	 * @param cells - sets an array of cells, as opposed to doing it one by one
	 */
	public void setCells(Cell[][] cells) {
		this.cells = cells;
	}

	@Override
	public Cell getCell(int x, int y) {
		if ( (x < 0 || x >= width) || (y < 0 || y >= height) )
			return null;
		return cells[x][y];
	}


	@Override
	public int getWidth() {
		return width;
	}

	@Override
	public int getHeight() {
		return height;
	}

}


